package com.zretc.emptraining.daoimpl;

import java.util.List;

import com.zretc.emptraining.dao.UsersDao;
import com.zretc.emptraining.dbtools.DBTools;
import com.zretc.emptraining.dbtools.MD5Util;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.entity.Dept;
import com.zretc.emptraining.entity.Teach;
import com.zretc.emptraining.entity.Users;

public class UsersDaoImpl implements UsersDao {

	@Override
	public List<Users> getUsers(String keywords) {
		// 获得用户列表
		String sql = "select *from users where userName like ?";
		return (List<Users>) DBTools.exQuery(sql, Users.class, '%' + keywords + '%');
	}

	@Override
	public boolean addUsers(Users users) {
		// 添加用户
		if (users == null) {
			return false;
		}
		String sql = "insert into users values(?,?,?,?,?)";
		return DBTools.getUpdate(sql, users.getUserNo(), users.getEmpNo(), users.getUserName(),
				MD5Util.getEncodeByMd5(users.getUserPwd()), users.getUserState()) > 0;
	}

	@Override
	public List<Users> getUsersByName(String userName) {
		// 通过用户名字精确查找
		String sql = "select * from users where userName = ?";
		return (List<Users>) DBTools.exQuery(sql, Users.class, userName);
	}

	@Override
	public boolean doLogin(String userName, String userPwd) {
		// 加密
		String md5Pwd = MD5Util.getEncodeByMd5(userPwd);
		boolean flag = true;
		// 调用dbtools的方法来完成验证
		List<Users> list = (List<Users>) DBTools.exQuery("select * from users where username=? and userpwd=?",
				Users.class, userName, md5Pwd);
		if (list.size() == 0) {
			return false;
		}
		return flag;
	}

	// 修改状态
	@Override
	public boolean stateUpd(String state, String userNo) {
		return DBTools.getUpdate("UPDATE users SET userState = ? where userNo = ?", state, userNo) > 0;
	}

	@Override
	public PageData<Users> getUserByPage(int page, int pageSize, String keywords) {
		String sql = "SELECT userNo,u.empNo,e.empName,userName,userPwd,userState\r\n"
				+ "FROM users u LEFT JOIN emp e\r\n" + "ON u.empNo = e.empNo WHERE u.userName like ?";
		PageData<Users> pd = DBTools.exQueryByPage(sql, Users.class, page, pageSize, "%" + keywords + "%");
		return pd;
	}

	// 修改用户信息
	@Override
	public boolean updUsersByName(int empNo, String userName, String userPwd, String userNo) {
		// TODO Auto-generated method stub
		return DBTools.getUpdate("UPDATE users SET empNo = ?,userName = ?,userPwd = ? where userNo = ?", empNo,
				userName, MD5Util.getEncodeByMd5(userPwd), userNo) > 0;
	}

	@Override
	public boolean initUserPwd(String userNo) {
		String sql = "update users set userPwd = ? where userNo = ?";
		return DBTools.getUpdate(sql, MD5Util.getEncodeByMd5("123456"), userNo) > 0;
	}

	@Override
	public boolean updUserNo(int empNo, String empTel) {
		String sql = "update users set userNo = ? where empNo = ?";
		return DBTools.getUpdate(sql, empTel, empNo) > 0;
	}

	@Override
	public boolean updUserPwd(String userNo, String userPwd) {
		String sql = "update users set userPwd = ? where userNo = ?";
		return DBTools.getUpdate(sql, MD5Util.getEncodeByMd5(userPwd), userNo) > 0;
	}

	@Override
	public boolean updPwd(String userName, String userPwd) {
		// 加密
		String md5Pwd = MD5Util.getEncodeByMd5(userPwd);
		return DBTools.getUpdate("UPDATE users SET userPwd = ? where userName = ?", md5Pwd, userName) > 0;
	}

	@Override
	public boolean checkPwd(int empNo, String pwd) {
		// 加密
		String md5Pwd = MD5Util.getEncodeByMd5(pwd);
		boolean flag = true;
		// 调用dbtools的方法来完成验证
		List<Users> list = (List<Users>) DBTools.exQuery("select * from users where empNo=? and userPwd=?", Users.class,
				empNo, md5Pwd);
		if (list.size() == 0) {
			return false;
		} else if (flag) {

		}
		return flag;
	}

	@Override
	public Users getUsersEmpNo(String userName) {
		String sql = "select *from users where userName = ?";
		List<Users> list = (List<Users>) DBTools.exQuery(sql, Users.class, userName);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public boolean updEmp(String empName, int deptNo, String empXl, String empSex, String empID, String empAddress,
			String empTel, String empEmail, String empJob) {
		// 通过名字修改员工信息
		String sql = "UPDATE emp SET deptNo = ?,empXl = ?,empSex = ?,empID = ?,empAddress = ?,empTel = ?,empEmail = ?,empJob = ? WHERE empName = ?";
		return DBTools.getUpdate(sql, deptNo, empXl, empSex, empID, empAddress, empTel, empEmail, empJob, empName) > 0;
	}


	@Override
	public List<Dept> getDeptNo(int empNo) {
		// 获取主管的部门号
		String sql = "select deptNo from dept where empNO=?";
		return (List<Dept>) DBTools.exQuery(sql, Dept.class, empNo);
	}

	@Override
	public List<Teach> getTeachNo(String userName) {
		String sql = "SELECT teach.teacherNo from teach where teacherTel = ?";
		return (List<Teach>) DBTools.exQuery(sql, Teach.class, userName);
	}
}
